package vn.mobisoft.gachno;

import java.io.IOException;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Customer extends Activity{
	private Button btn_exit;
	private Button btn_check;
	private TextView textView;
	
	
	private static final int 	DIALOG_DOWNLOAD_PROGRESS = 0;
	private ProgressDialog 		mProgressDialog;
	private LoginAsync 			lgThread;
	private Boolean 			connectResult = false;
	private String 				httpResutl;
	
	
	private String 				clientId;
	
	private String 				clientName;
	private String 				clientDeb;
	private String 				clientCode;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.customer);
        
        textView = (TextView)findViewById(R.id.c_textView);
        btn_exit = (Button)findViewById(R.id.c_button_exit);
        btn_exit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				((Global)getApplication()).getMain().updateLog();
				finish();				
			}
		});
        
        
        btn_check = (Button)findViewById(R.id.c_button_update);
        btn_check.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
    			lgThread = new LoginAsync();
    			lgThread.execute();
			}
		});
        
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			String sTemp = extras.getString("CLIENT_INFOR");
			sTemp = sTemp.replace("---", ":");
			String[] arr = sTemp.split(":");
			if (arr.length>=3) {
				clientName = arr[0];
				clientDeb = arr[1];
				clientCode = arr[2];
			}
			
			textView.setText("KH: "+ clientName +"\r\nSố nợ: "+ clientDeb +"\r\nMã thanh toán: " + clientCode);
			clientId = extras.getString("CLIENT_ID");
		}
    }
    
    
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_DOWNLOAD_PROGRESS:
			mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setTitle("");
			mProgressDialog.setMessage("Xin chờ trong giây lát!");
			mProgressDialog.setCancelable(true);
			mProgressDialog.show();
			return mProgressDialog;
		default:
			return null;
		}
	}

	 
	/*
	 * login thread
	 */
	class LoginAsync extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			showDialog(DIALOG_DOWNLOAD_PROGRESS);
		}

		@Override
		protected String doInBackground(String... aurl) {
			
			connectResult = false;
			httpResutl = "";	
			
/*			String sLink =	String.format(CONSTANT.LINK_UPDATE, 
							((Global)getApplication()).getCode(), 
							((Global)getApplication()).getPhone(),
							clientCode);*/
			
			String sLink =	String.format(CONSTANT.LINK_UPDATE, 
					"1394", 
					"1394",
					clientCode);
			try {
				httpResutl = HttpConnect.getHttpStream(sLink);
				System.out.println("sLink:"+sLink);
				System.out.println("httpResutl:"+httpResutl);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			if (httpResutl != null) {
				connectResult = true;
			}
			return null;
		}

		@Override
		protected void onCancelled() {

		}

		protected void onProgressUpdate(String... progress) {
			mProgressDialog.setProgress(Integer.parseInt(progress[0]));
		}

		@Override
		protected void onPostExecute(String unused) {
			if (!connectResult) {
				Toast.makeText(getApplicationContext(), "Lỗi kết nối!",
						Toast.LENGTH_SHORT);
			} else {
				checkResult();
			}
			dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
		}


	}

	public void checkResult() {

		if (httpResutl==null) {
			Toast.makeText(getApplicationContext(), "Lỗi kết nối!", Toast.LENGTH_LONG).show();
			return;
		}else {		
			if (httpResutl.indexOf("1")>=0) {
				Toast.makeText(getApplicationContext(), "Gạch nợ thành công!", Toast.LENGTH_LONG).show();
			}else {
				Toast.makeText(getApplicationContext(), "Gạch nợ lỗi!", Toast.LENGTH_LONG).show();
			}
		}	
	}
	
}
